/*
# ------------------------------------------------------------------------
# JD Base Theme
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

;(function($) {
	var options;
	var _backs = [];
	var _currenttoggle = null;
	var _last = null;

	var _links = null;
	var _back = null;
	var _close = null;
	var _title = null;
	var _boxes = null;
	var _boxes2 = null;
	var _overlay = null;
	var _mainbox = null;
	
	$.fn.JAIToolbox = function(args) {  
		options =  $.extend($.fn.JAIToolbox.defaults, args);  
		
		return this.each(function() {  
			start();
		});
	}
		
	$.fn.JAIToolbox.defaults = { 
		animOn:			false,
		axis:				'x',
		slideInterval: 0,
		slideSpeed: 20
	};
				
	start = function(){
		_links = $('a');
		_back = $('#toolbar-back');
		_close = $('#toolbar-close');
		_title = $('#toolbar-title');
		_boxes = $('.toolbox');
		_boxes2 = $('.toolbox');
		
		if (!$(_boxes).size()) return;
		_overlay = $('#ja-overlay');
		_mainbox = $('#ja-toolbar-main');
		
		if (options.animOn) {
			$(_boxes).css('opacity', 0);
			$(_overlay).css('opacity', 0);
			$(_boxes).push ($('#ja-toolbar-main'));
			$(_boxes).push (_overlay);
			//this._fx = new Fx.Elements (_boxes, {'onComplete':this.slidedone.bind(this)});
		}
		
		top = $(_boxes[0]).position().top;
		
		$(_links).each(function(i){
			if (this.href && this.hash && this.hash != "#") {
				//this._box = $(this.hash.substr(1));	
				this._box = $(this.hash);	
				if (!$(this._box).size() || !$(this._box).hasClass ('toolbox')) return;
				//$(_box)._link = this;
				this._h = top + $(this._box).outerHeight();
				if ($(this).hasClass('ip-button')) {
					$(this).bind('click', function(e){
						//new Event(e).stop();
						togglebox(this);
						return false;
					});
				} else {
					$(this).bind('click', function(e){
						//new Event(e).stop();
						showbox(this, true);
						return false;
					});
				}
			}
		});
		
		if ($(_back).size()) {
			$(_back).bind('click', function(e){
				//new Event(e).stop();
				back();
				return false;
			});
		}
		if ($(_close).size()) {
			$(_close).bind('click', function(e){
				//new Event(e).stop();
				close();
				return false;
			});
		}
		
		$(_overlay).bind('click', function(e) {
			close();
			return false;
		});
	};		
	
	slidedone = function() {
		if (_currenttoggle == null) {
			//hide overlay
			$(_overlay).css('display','none');
		}
	};	
	togglebox = function(link) {
		if (_currenttoggle == link) {
			close();
		}
		if (_currenttoggle == null) {
			//show overlay
			$(_overlay).css({'display':'block', 'height':$('#ja-wrapper').height()});
		}
		showbox (link, true);
		_currenttoggle = link;
	};		
	showbox = function(link, addback) {
		if (options.animOn) showbox2 (link, addback);
		else showbox1 (link, addback);
	};		
	close = function() {	
		if (options.animOn) close2 ();
		else close1 ();
	};			
	showbox1 = function(link, addback) {
		$(_boxes2).hide();
		$(link._box).show();
		$(_mainbox).css('height', link._h);
		
		if (addback && _last) {
			_backs.push (_last);
		}
		_last = link;
		updatestatus(link);
	};		
	close1 = function() {
		$(_boxes2).hide();
		$(_mainbox).css('height', 0);
		
		//hide overlay
		$(_overlay).css('display','none');

		_backs = [];
		_currenttoggle = null;
		_last = null;
	};		
	showbox2 = function(link, addback) {
		//$(_fx).stop();
		/*objs = {};
		for (i=0; i<$(_boxes).length - 2; i++) {
			if (_boxes[i] != link._box) {
				objs[i] = {'opacity': 0};
			} else {
				objs[i] = {'opacity': 1};
			}
		}
		
		objs[$(_boxes).length - 2] = {'height': link._h};
		if (_currenttoggle == null) {
			objs[$(_boxes).length - 1] = {'opacity': 0.7};
		}*/
		//$(_fx).start (objs);
		$(_boxes).fadeIn(3000);
		
		if (addback && _last) {
			_backs.push (_last);
		}
		_last = link;
		updatestatus(link);
	};		
	close2 = function() {
		//$(_fx).stop();
		/*objs = {};
		for (i=0; i<$(_boxes).length - 2; i++) {
			objs[i] = {'opacity': 0};
		}
		
		objs[$(_boxes).length - 2] = {'height': 0};
		objs[$(_boxes).length - 1] = {'opacity': 0};*/
		
		//$(_fx).start (objs);	
		$(_boxes).fadeOut();

		_backs = [];
		_currenttoggle = null;
		_last = null;
	};		
	updatestatus = function(link) {
		$(_title).html(link.title);
		if ((lastlink = _backs[_backs.length - 1])) {
			$(_back).html(lastlink.title);
			$(_back).show();
		} else {
			$(_back).html('');
			$(_back).hide();
		}
	};		
	back = function() {
		if ((link = _backs.pop())){
			showbox (link);
		}
	};

	
})(jQuery);
